#ifndef GAMEROOM_H
#define GAMEROOM_H

#include <QObject>
#include <mutex>

class QTcpSocket;
struct User : public QObjectUserData {
  QString nick;
  QString room;
  QString word;
  bool leave = false;
  bool out = false;
  QTcpSocket *sock = nullptr;
  bool created;
  QList<QString> talks;
};

void writeSocket(QTcpSocket *socket, const QJsonObject &data);
void writeSocket(QTcpSocket *socket, const QByteArray &data);

class GameRoom : public QObject {
  Q_OBJECT
public:
  explicit GameRoom(const QString &roomName, QObject *parent = nullptr);

  const QString &roomName();

  bool exist(const QString &nick);

  bool join(User *user);

  bool leave(User *user);

  void notify(const QString &cmd, const QString &msg = nullptr);
  void notifyOne(User *user, const QString &cmd, const QString &msg = nullptr);
  void say(int idx = -1);
  void vote(const QString &nick, const QString &other);

  void syncNotify();

  User *user(const QString &nick);

  bool joinRoomHandler(QJsonValue &req, QTcpSocket *sock);

  QJsonObject toJsonObject();

  void allocWord();

  void setNumber(int alive, int underver) {
    last = alive, m_undercover = underver;
  }

signals:

public slots:

private:
  QString m_roomName;
  QList<User *> users;
  QList<QPair<QString, QString>> votes;
  std::mutex muxVote;
  bool palyed = false;
  int sayIdx;
  int last, m_undercover;
  QString undercoverKey;
  QString plebeian;
  const QString m_words = "王菲,那英;元芳,展昭;麻雀,乌鸦";
};

#endif // GAMEROOM_H
